#include<iostream>

using namespace std;

struct ListNode{
    int value;
    ListNode * next;
};

ListNode * createNode(int * data, int length){
    if ( data == nullptr || length <= 0 )
        return nullptr;
    ListNode * head = new ListNode();
    head -> value = data[0];
    head -> next = nullptr;
    ListNode * pHead = head;
    for ( int i=1;i<length; i++ ){
        ListNode * node = new ListNode();
        node -> value = data[i];
        node -> next = nullptr;
        pHead = pHead -> next = node;
    }
    return head;
}

void deleteDuplication(ListNode ** head){
if ( *head == nullptr || head == nullptr )
	return;
    ListNode * pNode = *head;
    ListNode * preNode = nullptr;
    while ( pNode != nullptr ){
        ListNode * nextNode = pNode -> next;
        // 标记是否有重复值
        bool flag = false;
	ListNode * deleteNode = nullptr;
        while ( nextNode != nullptr &&  nextNode -> value == pNode->value ){
            flag = true;
		deleteNode = nextNode;
            nextNode = nextNode -> next;
		delete deleteNode;
        }

        if ( !flag ){
            // 无重复节点
            preNode = pNode;
		pNode = pNode -> next;
        }else{
            // // 判断是否是头结点重复
            if ( preNode == nullptr ){
                *head = nextNode;
                pNode = nextNode;
            }else {
                preNode -> next = nextNode;
                pNode = nextNode;
            }
        }
    }
}

void printNode(ListNode * head){
    ListNode * pHead = head;
    while ( pHead != nullptr ){
        cout << pHead->value << endl;
        pHead = pHead -> next;
    }
}

int main(void){
    // Test1
    int length = 20;
    int data[] = {1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,0,0};
    // Test2
    int length = 13;
    int data[] = {1,1,2,3,4,5,5,6,7,8,9,0,0};
    // Test3
    int length = 10;
    int data[] = {1,2,3,4,5,6,7,8,9,0};
    // Test4
    int length = 0;
    int data[] = {};
    ListNode * head = createNode( data, length);
    printNode(head);
    deleteDuplication(&head);
    cout << "------------------" << endl;
    printNode(head);
    return 0;
}